import aqv.Role
import aqv.User
import aqv.UserRole
import aqv.PoliticalParty
import aqv.PoliticalPartyRole
import aqv.Politician
import grails.util.Environment


class BootStrap {

    def init = { servletContext ->
		
		//println "Environment.getCurrent() : "  + Environment.getCurrent()
		
		switch(Environment.getCurrent()) {
			case "DEVELOPMENT":
				
				// Politician Roles
				try {
					println "Initialize -- Politician Roles"
					["Dirigencia", "Candidatos", "Cuerpo Administrativo", "Asesores"
						, "Militantes", "Afiliados", "Simpatizante"].each{
						def politicalPartyRole = new PoliticalPartyRole(tittle: it)
						politicalPartyRole.save()
					}
					println "Finished 	-- Politician Roles"
				} catch(Exception e) {
					println "An error ocurred while Initializing Politician Roles"
				}
			
				// Politician parties
				try {
					println "Initialize -- Politician Parties"
					
					def role = PoliticalPartyRole.get(1)
					def party = PoliticalParty.get(1)
					
					["Party 1" , "Party 2", "Party 3"].each {
						
						
						def adminUser = new User(
							username:'admin'+it,
							password: 'password',
							enabled:true,
							accountExpired: false,
							accountLocked: false,
							passwordExpired: false,

							firstName:'admin name'+it,
							lastName:'admin last'+it,
							email: 'test',
							politicalParty: party,
							politicalPartyRole: role,
							apiKey: 'test',
							confirm: false
						)
						
						adminUser.save()
						
						
						
						def politcalParty = new PoliticalParty(
							name : it,
							description : "descr "+ it,
							cantOfPoliticians: 0,
							admin: adminUser
						)
						politcalParty.save()
					}
					println "Finished 	-- Politician Parties"
				} catch(Exception e) {
					println "An error ocurred while Initializing Politician Parties"
				}
				
				// Politician
				try {
					println "Initialize -- Test Politician"
					
						def role = PoliticalPartyRole.get(1)
						def party = PoliticalParty.get(1)
						def dateBirth = new Date()
						
						def politician = new Politician(
							name: 'polName', 
							surName:'surName',
							politicalParty: party, 
							politicalPartyRole: role,
							profilePic: null,
							birthDate: dateBirth, 
							description: 'this is a description'
						)
						politician.save()

					println "Finished 	-- Test Politician"
				} catch(Exception e) {
					println "An error ocurred while Initializing Test Politician"
				}
				
				// User
				try {
					println "Initialize -- Test User"
					
						def role = PoliticalPartyRole.get(1)
						def party = PoliticalParty.get(1)
						
						def user = new User(
							username:'username',
							password: 'password',
							enabled:true,
							accountExpired: false,
							accountLocked: false,
							passwordExpired: false,

							firstName:'firstName',
							lastName:'lastName',
							email: 'test',
							politicalParty: party,
							politicalPartyRole: role,
							apiKey: 'test',
							confirm: false
						)
						
						user.save()
						println "Finished 	-- Test User"
						
						try {
							println "Initialize -- User Role assigment"
							
							def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
							def userRole = new Role(authority: 'ROLE_USER').save(flush: true)
					  
							UserRole.create(user, adminRole, true)
							
							println "Finish -- User Role assigment"
						} catch (Exception e) {
						println "An error ocurred while assigning Test User Roles: \n" + e.message
						}
						
						
						//session.user = user
					
				} catch(Exception e) {
					println "An error ocurred while Initializing Test User: \n" + e.message
				}
				
				
				
					
					
				
				// initialize in development here
				break
			case "PRODUCTION":
			
				
			
			
				try {
					println "Initialize -- Politician Roles"
					["Dirigencia", "Candidatos", "Cuerpo Administrativo", "Asesores"
						, "Militantes", "Afiliados", "Simpatizante"].each{
						def politicalPartyRole = new PoliticalPartyRole(tittle: it)
						politicalPartyRole.save()
					}
					println "Finished 	-- Politician Roles"
				} catch(Exception e) {
					println "An error ocurred while Initializing Politician Roles"
				}
			
				try {
					println "Initialize -- Politician Parties"
					["Party 1" , "Party 2", "Party 3"].each {
						def politcalParty = new PoliticalParty(
							name : it,
							description : "descr "+ it,
							cantOfPoliticians: 0
						)
						politcalParty.save()
					}
					println "Finished 	-- Politician Parties"
				} catch(Exception e) {
					println "An error ocurred while Initializing Politician Parties"
				}
				
				try {
					println "Initialize -- Test Politician"
					
						def role = PoliticalPartyRole.get(1)
						def party = PoliticalParty.get(1)
						def dateBirth = new Date()
						
						def politician = new Politician(
							name: 'polName', surName:'surName',
							politicalParty: party, politicalPartyRole: role,
							profilePic: null,
							birthDate: dateBirth, description: 'this is a description')
						politician.save()
						
					
					println "Finished 	-- Test Politician"
				} catch(Exception e) {
					println "An error ocurred while Initializing Test Politician"
				}
				
				
				try {
					println "Initialize -- Test User"
					
						def role = PoliticalPartyRole.get(1)
						def party = PoliticalParty.get(1)
						def user = new User(login:'login',password: 'password',
							firstName: 'firstName', lastName:'lastName',
							politicalParty: party, politicalPartyRole: role)
						user.save()
						
						session.user = user
					
					println "Finished 	-- Test User"
				} catch(Exception e) {
					println "An error ocurred while Initializing Test User"
				}
				
			
				break
			case "TEST":
				break
		}
	
    }
    def destroy = {
    }
}
